<?
	/* Identica/laconica - stream reader plugin */	

	// return type of service
	function identica_getType() { return "status"; }


	// extra the service- action  line ("ike: "I tweeted on twitter.com")
	function identica_getAction($line) {
		return "<a href='?service=identica'><img class='icon' src='".serviceIcon("identica")."'/></a> I updated my status on <a href='http://identi.ca'>identi.ca</a>";
	}
	
	// extract and return the correct caption
	function identica_getCaption($line) {
		$entry=explode("|",$line);
		return $entry[3];
	}

	// extract and return the correct post
	function identica_getPost($line) {
		$entry=explode("|",$line);
		return ""; //$entry[2];
	}

	// extract and return the correct link to the item
	function identica_getLink($line) {
		$entry=explode("|",$line);
		return $entry[5];
	}	


	// display information about us with a link to the profile
	//  -- this is called to display list of services that are linked (admin-functions.php)
	function identica_identify($item) {
		$entry=explode("|",$item);
		return "Identica account <a href='".$entry[2]."'>".$entry[1]."</a>";
	}
	
	// get service + profile information
	function identica_addProfile() {
	
			$serviceID=$_POST['addservice'];
			$username=$_POST['username'];
			
			writeService($serviceID,$username,"http://identi.ca/".$username."/rss");
	}
	
	function identica_download($serviceid,$filename)  {

		$reader = new XMLReader();
		$reader->open($filename );

			
		set_time_limit(30);


		$count=0;
		
		$itemids=array();
		$continueParsing=true;

		while ($reader->read() && $continueParsing) {
				
				if ($reader->nodeType==XMLREADER::ELEMENT) {

					if ($reader->name=="item") {
						$guid=null;
						$title=null;
						$mediaurl=null;
						$thumb=null;
						$link=null;
						$pubdate=null;
						$content=null;
						
						$guid=$reader->getAttribute("rdf:about");
						
						while ($reader->read()) {

							/*
							if ($reader->localName=="link" && !$link) {
								$reader->read();
								$link=$reader->value;
							}
							*/
							
							if ($reader->name=="dc:date" && !$pubdate) {
								$reader->read();
								$pubdate=$reader->value;
								$pubdate=date("r",strtotime($pubdate));
							}

							if ($reader->name=="title" && !$title) {
								$reader->read();
								$title=$reader->value;
							}
		

							if ($reader->name=="link" && !$link) {
								$reader->read();
								$link=$reader->value;
								//$guid=$mediaurl;
							}
							
							if ($reader->localName=="item") {
								if ($guid) {
								
									$indx=date("YmdHis",strtotime($pubdate));
									
									// see if this is an @ reply, and then skip it
									$username=substr($title,0,strpos($title,":"));
									$title=substr($title,strpos($title,":")+2);

									
									if (substr($title,0,1)!="@" || $includeATtweets) {
										if (newGuid($guid)) {
											$item[$indx]->title=$title;
											$item[$indx]->post=$title;
											$item[$indx]->link=$link;
											$item[$indx]->pubDate=$pubdate;
											$item[$indx]->guid=$guid;
											$item[$indx]->extra="|".$link."|".$username;
										} else $continueParsing=false;
									}
								}
								break;
							}
						}
						
						//print $item[$guid]["title"]." : <img src='".$item[$guid]["media"]."'>";
					}
					set_time_limit(30);
				}
				
		}  
		
		insertEntries($item,"identica",$serviceid);
	}
?>